<template>
  <div class="zzz">
    <filterPanel filterName="GOrganizationListFilter" filterTitle="" :filterList="filterData"
                 @filterChange="filterChange"></filterPanel>
    <tablePanel tableName="GOrganizationListTable" :tableLoading="loading" tableTitle="" :tableList="tableList"
                :tableData="tableData" :total="total" :currentPage="form.page_start" :pageSize="form.page_size"
                @sizeChange="sizeChange" @pageChange="pageChange" @tableClick="tableClick">
      <zy-button @click="add" v-if="isAdd" type="primary">新增组织</zy-button>
      <zy-button @click="exportExcel">导出列表</zy-button>
    </tablePanel>
    <zyModel :show="show" :title="modelTitle" @submit="submit" @close="close">
      <div class="organizationChange">
        <zy-modal-info cell-num="2" :model-info="modelInfo"></zy-modal-info>
      </div>
    </zyModel>
  </div>
</template>

<script>
	import {mapGetters} from "vuex";

	export default {
		name: "GOrganizationList",
		props: {},
		data() {
			return {
				modelTitle: '',
				show: false,
				department_id: '',
				modelInfo: [
					{
						title: '组织名称',
						name: 'department_name',
						type: 'input',
						value: '',
						readonly: false,
						titleClass: 'required',
					},
					{
						title: '所属上级',
						name: 'parent_id',
						type: 'cascader',
						value: [],
						optionList: [],
						optionProps: {
							checkStrictly: true,
							value: 'value',
							label: 'label',
							children: 'children'
						},
						showAll: true,
						readonly: false,
						titleClass: 'required',
					},
				],
				currentPath: '/depart_manager',
				filterData: [
					{
						title: '组织名称',
						name: 'department_name',
						type: 'input',
						value: '',
						isShow: true,
						isSelected: true
					},
					{
						title: '所属上级',
						name: 'parent_name',
						type: 'input',
						value: '',
						isShow: true,
						isSelected: true
					},
					// {
					// 	title: '访问路径',
					// 	name: 'user',
					// 	type: 'input',
					// 	value: '',
					// 	isShow: true,
					// 	isSelected: true
					// },
					{
						title: '创建时间',
						name: 'time',
						type: 'date',
						value: [],
						isShow: true,
						isSelected: true
					},
				],
				form: {
					department_name: '',
					parent_name: '',
					time: [],
					page_start: 1,
					page_size: 10
				},
				loading: false,
				tableList: [
					{
						title: '组织名称',
						width: 200,
						name: 'department_name',
						isShow: true,
						isSelected: true
					},
					{
						title: '所属上级',
						width: 200,
						name: 'parent_name',
						isShow: true,
						isSelected: true
					},
					{
						title: '创建时间',
						width: 200,
						name: 'ctime',
						isShow: true,
						isSelected: true
					},
					{
						title: '操作',
						limitWidth: 160,
						name: 'handler',
						fixed: 'right',
						btns: [
							{
								type: 'modify',
								name: '修改'
							},
							{
								type: 'delete',
								name: '删除',
								btnType: 'danger'
							},
						],
						isSelected: true
					}
				],
				tableData: [],
				total: 0,
				download_url: ''
			};
		},
		computed: {
			postData() {
				let data = {
					department_name: this.form.department_name,
					parent_name: this.form.parent_name,
					pagination: {
						page_start: this.form.page_start,
						page_size: this.form.page_size
					}
				};
				if (this.form.time && this.form.time.length > 0) {
					data.start_time = this.form.time[0] / 1000;
					data.end_time = this.form.time[1] / 1000;
				}
				return data;
			},
			...mapGetters(['authority']),
			isAdd() {
				return this.authority[this.currentPath] && this.authority[this.currentPath].i;
			},
			isModify() {
				return this.authority[this.currentPath] && this.authority[this.currentPath].m;
			},
			isDel() {
				return this.authority[this.currentPath] && this.authority[this.currentPath].d;
			},
		},
		watch: {},
		components: {},
		methods: {
			getList() {
				this.$axios.post(this.$api.admin_departmentTree, {}).then(res => {
					let {code, data, msg} = res;
					if (code == 200) {
						this.modelInfo = this.modelInfo.filter(item => {
							if (item.name == 'parent_id') {
								item.optionList = data.filter(item => {
									return item;
								}) || []
							}
							return item;
						})
					} else {
						this.$message.error(msg);
					}
				})
			},
			submit() {
				let postData = {
					department_id: this.department_id || ''
				}
				this.modelInfo.forEach(item => {
					if (item.name == 'parent_id') {
						postData[item.name] = item.value[item.value.length - 1]
					} else {
						postData[item.name] = item.value;
					}
				});
				this.$axios.post(this.$api.admin_entryDepartment, {...postData}).then(res => {
					let {code, msg} = res;
					if (code == 200) {
						this.$message.success("修改成功!");
						this.close(true);
					} else {
						this.$message.error(msg);
					}
				})
			},
			close(isRefresh) {
				this.department_id = '';
				this.show = false;
				this.modelInfo.forEach(item => {
					if (item.name == 'parent_id') {
						item.value = []
					} else {
						item.value = ''
					}
				})
				if (isRefresh) this.getData();
			},
			add() {
				this.show = true;
				this.modelTitle = '新增组织'
			},
			//导出
			exportExcel() {
				this.$import_excal(this.download_url || "");
			},
			sizeChange(val) {
				this.form.page_size = val;
				this.getData()
			},
			pageChange(val) {
				this.form.page_start = val;
				this.getData()
			},
			filterChange(data) {
				this.form = {...this.form, ...data};
				this.getData();
			},
			getData() {
				this.loading = true;
				this.tableData = [];
				this.$axios.post(this.$api.admin_listDepartment, {...this.postData}).then(res => {
					let {code, msg, data} = res;
					this.loading = false;
					if (code == 200) {
						this.tableData = data.list.filter(item => {
							item.ctime = this.$Timestamp(item.create_time) || ''
							item.modify = this.isModify;
							item.delete = this.isDel;
							return item;
						});
						this.download_url = data.download_url || ''
						this.total = data.pagination.total_count || 0;
					} else {
						this.tableData = []
						this.total = 0;
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			tableClick(data, type) {
				let {department_id} = data;
				switch (type) {
					case 'modify':
						this.modelTitle = '修改组织';
						this.department_id = department_id;
						this.modelInfo.forEach(item => {
							for (let key in data) {
								item.value = data[item.name];
							}
						})
						this.show = true;
						break;
					case 'delete':
						this.deleted(department_id)
						break;
				}
			},
			toModify() {
			},
			deleted(department_id) {
				this.$confirm("此操作将删除该部门, 是否继续?", "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning",
					customClass: 'message',
				}).then(() => {
					this.$axios.post(this.$api.admin_delDepartment, {department_id}).then(res => {
						let {code, msg} = res;
						if (code === 200) {
							this.$message.success('删除成功！')
							this.getData();
						} else {
							this.$message.error(msg);
						}
					})
				}).catch(() => {
					this.$message.info('已取消删除')
				});
			}
		},
		mounted() {
			this.getList();
			this.getData();
		},
		created() {
		}
	}
</script>

<style scoped lang="less" src="@/style/government/systemManage/organizationList/index.less">

</style>
